//
//  UIAlertController+ChangeColor.m
//  insuranceAgent
//
//  Created by 聂志康 on 2018/4/17.
//  Copyright © 2018年 longhuantz. All rights reserved.
//

#import "UIAlertController+ChangeColor.h"

@implementation UIAlertController (ChangeColor)
+ (UIAlertController *)showAlertControllerWithTitle:(NSString *)title message:(NSString *)message andTextColor:(UIColor *)color tintAll:(BOOL)tintAll action:(void(^)(UIAlertAction *action))actionTarget {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (actionTarget) {
            actionTarget(action);
        }
    }];
    [alert addAction:action];
    // 取消按钮
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:actionCancel];
    if (color) {
        if (tintAll) {
            //修改title
            NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
            [alertControllerStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, title.length)];
//            [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, title.length)];
            [alert setValue:alertControllerStr forKey:@"attributedTitle"];
            
            //修改message
            NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
            [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, message.length)];
//            [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, message.length)];
            [alert setValue:alertControllerMessageStr forKey:@"attributedMessage"];
        }
        
        //修改选择按钮
        [action setValue:color forKey:@"_titleTextColor"];
        //修改取消按钮
        [actionCancel setValue:color forKey:@"_titleTextColor"];
    }
    return alert;
    //[UIColor colorWithRed:(254 / 255.0) green:(137 / 255.0) blue:(203 / 255.0) alpha:1.0]
}

+ (UIAlertController *)showAlbumAlertControllerWithAlbumAction:(void(^)(UIAlertAction *action))albumActionTarget cameraAction:(void(^)(UIAlertAction *action))cameraActionTarget andTextColor:(UIColor *)color {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (cameraActionTarget) {
            cameraActionTarget(action);
        }
    }];
    [alert addAction:cameraAction];
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (albumActionTarget) {
            albumActionTarget(action);
        }
    }];
    [alert addAction:albumAction];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    if (color) {
        [cameraAction setValue:color forKey:@"_titleTextColor"];
        [albumAction setValue:color forKey:@"_titleTextColor"];
        [action setValue:color forKey:@"_titleTextColor"];
    }
    [alert addAction:action];
    return alert;
}

// 取消按钮
- (void)addCancelActionTarget:(UIAlertController*)alertController color:(UIColor *)color title:(NSString *)title
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:nil];
    [action setValue:color forKey:@"_titleTextColor"];
    [alertController addAction:action];
}

// 选择按钮
- (void)addActionTarget:(UIAlertController *)alertController title:(NSString *)title color:(UIColor *)color action:(void(^)(UIAlertAction *action))actionTarget
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        actionTarget(action);
    }];
    [action setValue:color forKey:@"_titleTextColor"];
    [alertController addAction:action];
}


@end
